package com.example.demo.services.impl;

import com.example.demo.common.AppResult;
import com.example.demo.common.ResultCode;
import com.example.demo.dao.BoardMapper;
import com.example.demo.exception.ApplicationException;
import com.example.demo.model.Board;
import com.example.demo.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Autowired
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<=0)
        {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boardList=boardMapper.selectByNum(num);

        return boardList;
    }

    @Override
    public Board selectById(Long id) {
        if(id==null || id<=0)
        {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        Board board=boardMapper.selectByPrimaryKey(id);

        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {

        if(id==null || id<=0)
        {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        Board board=boardMapper.selectByPrimaryKey(id);
        if(board==null)
        {
            log.warn(ResultCode.ERROR_IS_NULL.toString()+",board id="+id);
            throw  new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }

        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int row=boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1)
        {
            log.warn(ResultCode.FAILED.toString()+",受影响的行数大于一，操作失败！！！");
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null || id<=0)
        {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //查询板块详情
        Board board=boardMapper.selectByPrimaryKey(id);
        if(board==null)
        {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString()+",board id="+id);
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        ///构造新对象
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(updateBoard.getArticleCount()<0)
        {
            updateBoard.setArticleCount(0);
        }
        int row=boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1)
        {
            log.warn(ResultCode.FAILED.toString()+",受影响的行数大于一，操作失败！！！");
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
